package com.zhifa.Util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhifa.entity.Auth;
import com.zhifa.service.AuthService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ListToJasonArray {
	
	

	public static JSONArray listToJasonArray(AuthService authService,Map<String, Object>map){
		List<Auth> list = authService.findAuth(map);
		JSONArray jsonArray=new JSONArray();
		for (int i = 0; i <list.size(); i++) {
			Auth auth = list.get(i);
			JSONObject jsonObject=new JSONObject();
			jsonObject.put("id",auth.getId() );
			jsonObject.put("text", auth.getAuthname());
			jsonObject.put("state", auth.getState());
			jsonObject.put("iconCls", auth.getIcon());
			JSONObject attributeObject=new JSONObject();
			attributeObject.put("authPath", auth.getAuthpath());
			jsonObject.put("attributes", attributeObject);
			jsonArray.add(jsonObject);
		}
		return jsonArray;
	}
	
	public static JSONArray getTreeJsonArray(AuthService authService,Map<String, Object>map){
		
		JSONArray treeJasonArray = listToJasonArray(authService,map);
		for (int i = 0; i <treeJasonArray.size(); i++) {
			JSONObject jsonObject = treeJasonArray.getJSONObject(i);
			if("open".equals(jsonObject.getString("state"))){
				continue;
			}else{
				Map<String, Object>m=new HashMap<String, Object>();
				m.put("parentid", jsonObject.getString("id"));
				m.put("ids", map.get("ids"));
				jsonObject.put("children", listToJasonArray(authService,m));
			}
		}
		return treeJasonArray;
		
	}
	
}
